#include "StdAfx.h"
#include "ToolFun.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
//#include <sys/syslog.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
//#include <time.h>

CToolFun::CToolFun(void)
{
}

CToolFun::~CToolFun(void)
{
}

int CToolFun::daemon(const char *pname, int facility)
{
	int pid;
	int i;

	// ignore possible terminal signal.
	signal(SIGTTOU, SIG_IGN);
	signal(SIGTTIN, SIG_IGN);
	signal(SIGTSTP, SIG_IGN);
	signal(SIGHUP, SIG_IGN);

	// create child process, exit parent process.
	if ((pid=fork()))
		exit(EXIT_SUCCESS); // exit parent process
	else if (pid<0)
	{
		perror("fork error!\n");
		exit(EXIT_FAILURE);
	}

	setsid();   // set new group session , leave terminal

	for (i=0; i<256; i++)
		close(i);

	open("/dev/null", O_RDONLY);
	open("/dev/null", O_RDWR);
	open("/dev/null", O_RDWR);

	chdir("/");

	umask(0);

	signal(SIGCHLD, SIG_IGN);

	openlog(pname, LOG_PID, facility);

	return 0;
}

// use: string str = cmd_system("ls -l");
//      printf("return text: %s\n", str.c_str());
string CToolFun::cmd_system(const char *cmd)
{
	string str = "";
	FILE *fp = NULL;
	fp = popen(cmd, "r");
	char buf[4096] = {0};
	read(fp, buf, 4095);
	str = buf;
	if (fp != NULL)
		fclose(fp);

	return str;
}

/*/
int CToolFun::pwdchg(int argc, char** argv)
{
	struct passwd *userlist;
	int count, usernumber;
	FILE *tmp, *stmp, *mailer, *profile;
	char *commentfield, *username, *userdir, *home;
	char *mailcomment, *mailemail, reply;

	commentfield = (char*)malloc(1024*sizeof(char));
	username = (char*)malloc(8*sizeof(char));
	userdir = (char*)malloc(256*sizeof(char));
	home = (char*)malloc(256*sizeof(char));
	mailcomment = (char*)malloc(1024*sizeof(char));
	mailemail = (char*)malloc(512*sizeof(char));

	if (argc != 4)
	{

		printf("usage: %s [ dirname - no slashes ] [ logname ] [ comment - in quotes ]\n", argv[0]);
		return 1;
	}

	if (strlen(argv[2]) > 8 || strlen(argv[2]) < 5)
	{
		printf("Please choose a logname between 5 and 8 characters!\n");
		return 1;
	}

	signal(SIGHUP, SIG_IGN);
	signal(SIGINT, SIG_IGN);

	setpwent();

	count = 0;

	while ((userlist = getpwent()) != NULL)
	{
		if (count < userlist->pw_uid)
		{
			count = userlist->pw_uid;
			usernumber = count + 1;
		}
	}

	endpwent();

	sprintf(commentfield, "%s", argv[3]);
	sprintf(userdir, "%s", argv[1]);
	sprintf(username, "%s", argv[2]);
	sprintf(home, "/%s/%s", argv[1], argv[2]);

	printf("/n");
	printf("Check this out before proceeding!!!\n");
	printf("---------------\n");
	printf("Logname:\t%s\n", username);
	printf("Homedir:\t/%s/%s\n", userdir, username);
	printf("Comment:\t%s\n", commentfield);
	printf("---------------\n");
	printf("\n");

	printf("\n");
	printf("All of this ok?\n");
	printf("\n");
	printf("y or n [n is the default]\n");
	printf("\n");

	scanf("%c", &reply);

	if (reply != 'y')
	{
		printf("\n");
		printf("All right, give it another shot if you want!\n");
		return 0;
	}

	tmp = fopen("/etc/passwd", "a");
	fprintf(tmp, "%s:x:%d:1:%s:/%s/%s:/bin/sh\n", username, usernumber, commentfield, userdir, username);
	fclose(tmp);

	mkdir(home, 0755);
	chdir(home);

	profile = fopen(".profile", "a");
	fprintf(profile, "stty istrip\n");
	fprintf(profile, "PATH=/bin:/usr/bin:/usr/local/bin:/usr/share/bin:.\n");
	fprintf(profile, "export PATH\n");
	fprintf(profile, "\n\n");
	fclose(profile);

	chown(home, usernumber, 1);
	chown(".profile", usernumber, 1);
	chmod(".profile", 0644);

	if ((mailer = popen("/usr/lib/sendmail -t", "w")) == NULL)
	{
		perror("Mailer");
		return 1;
	}

	sprintf(mailcomment, "%s\n", commentfield);
	sprintf(mailemail, "The email address is %s@host.com!\n", username);


	fputs("To: bob@host.com\n", mailer);
	fputs("Subject: New User Added!!!\n", mailer);
	fputs("\n", mailer);
	fputs(mailcomment, mailer);
	fputs("has a new account set up!\n", mailer);
	fputs("\n", mailer);
	fputs("Thank you, \n", mailer);
	fputs("\t Johny Ma\n", mailer);
	pclose(mailer);

	printf("\nAll Done!!!\n");
	printf("\nNow set the Password!\n");
	printf("\n");
	execl("/usr/bin/passwd", "passwd", username, NULL);
	printf("\nPassword set!!! Take a break...\n");

	return 2;
}
//*/
